/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import bean.business.classBean;
import bean.business.studentBean;
import entities.Classes;
import entities.Student;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author HoangHai
 */
@ManagedBean
@RequestScoped
public class manageStudent {

    @EJB
    private classBean classBean;
    @EJB
    private studentBean studentBean;
    private Student stu = new Student();
    private Student stu1 = new Student();
    private List<Student> list = new LinkedList<Student>();
    private Double mark;

    public Double getMark() {
        return mark;
    }

    public void setMark(Double mark) {
        this.mark = mark;
    }
    public Student getStu1() {
        return stu1;
    }

    public void setStu1(Student stu1) {
        this.stu1 = stu1;
    }
    private int classID;

    public int getClassID() {
        return classID;
    }

    public void setClassID(int classID) {
        this.classID = classID;
    }

    public List<Student> getList() {
        list = studentBean.allStudent();
        return list;
    }

    public void setList(List<Student> list) {
        this.list = list;
    }

    public Student getStu() {
        return stu;
    }

    public void setStu(Student stu) {
        this.stu = stu;
    }

    /** Creates a new instance of manageStudent */
    public manageStudent() {
    }

    public void insert() {
        if (checkID() && checkEmail()) {
            Classes c = classBean.findClassByID(classID);
            //stu1.setClassID(c);
            studentBean.addStudent(stu1);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucessful", "Sucessful"));
        }
    }

    public void update() {
        if (checkEmail1()) {
            studentBean.updateStudent(stu);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucessful", "Sucessful"));
        }
    }

    public void delete() {
        studentBean.delStudent(stu.getStuID());
    }

    public boolean checkID() {
        List<Student> students = new LinkedList<Student>();
        students = studentBean.allStudent();
        for (Student student : students) {
            if (student.getStuID().equals(stu1.getStuID())) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "ID had been Already !"));
                return false;
            }
        }
        return true;
    }

    public boolean checkEmail() {
        Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
        Matcher m = p.matcher(stu1.getStuEmail());
        if (m.matches()) {
            return true;
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Email invalid!"
                    + "\n 'example@ex.com'"));
            return false;
        }
    }
    public boolean checkEmail1() {
        Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
        Matcher m = p.matcher(stu.getStuEmail());
        if (m.matches()) {
            return true;
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Email invalid!"
                    + "\n 'example@ex.com'"));
            return false;
        }
    }
}
